package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.news.GroupChooseActivity;
import com.renwei.yunlong.bean.GroupChooseBean;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.UtilList;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Project :  yunna.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2018/1/12 13:34
 * Changed by :  Benjamin.
 * Changed time: 2018/1/12 13:34
 * Class description:选择职能
 */

public class SelectGroupAdapter extends RecyclerView.Adapter<SelectGroupAdapter.ViewHolder> {
    private List<String> groupIdArray;
    private List<GroupChooseBean.RowsBean> mList;
    private Context mContext;
    public SelectGroupListenter listenter;

    public interface SelectGroupListenter {
        void groupSelect(String id, String name);
    }


    public void setListenter(SelectGroupListenter listenter) {
        this.listenter = listenter;
    }

    public void refresh(List<GroupChooseBean.RowsBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public SelectGroupAdapter(Context mContext) {
        this.mContext = mContext;
    }


    private boolean hasGroup(String groupId) {
        if (GroupChooseActivity.groupMap == null) {
            return false;
        } else {
            return StringUtils.value(GroupChooseActivity.groupMap.get(groupId)).length() != 0;
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_function_group_select, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final String name = StringUtils.value(mList.get(position).getUserGroupName());
        final String id = String.valueOf(mList.get(position).getUserGroupId());
        holder.tvFunctionName.setText(name);
        if (hasGroup(id)) {
            holder.ivFunctionChecke.setImageResource(R.mipmap.check02);
        } else {
            holder.ivFunctionChecke.setImageResource(R.mipmap.check03);
        }
        holder.itemFunction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listenter.groupSelect(id, name);
            }
        });
    }

    @Override
    public int getItemCount() {
        return UtilList.getCount(mList);
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_function_name)
        TextView tvFunctionName;
        @Bind(R.id.iv_function_checke)
        ImageView ivFunctionChecke;
        @Bind(R.id.item_function)
        LinearLayout itemFunction;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
